<?php

namespace app\admin\controller\statistics;

use app\common\controller\Backend;
use think\Db;
use think\Env;

/**
 * 统计图表示例
 *
 * @icon   fa fa-charts
 * @remark 展示在FastAdmin中使用Echarts展示丰富多彩的统计图表
 */
class Echarts extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('AdminLog');
    }

    /**
     * 查看
     */
    public function index()
    {
        $where = [];
        $wheres = [];
        $where_x = [];
        $wheres_x = [];
        $where_y = [];
        $wheres_y = [];
        if(isset(request()->param()['year'])){
            $year = request()->param()['year'];
            $year_arr = explode('~',$year);
            $start_time = $year_arr[0];
            $end_time = $year_arr[1];
            $where['create_time'] = ['>=',strtotime($start_time)];
            $wheres['create_time'] = ['<=',strtotime($end_time)];
            $where_x['entry_time'] = ['>=',strtotime($start_time)];
            $wheres_x['entry_time'] = ['<=',strtotime($end_time)];
            $time = $start_time.'~'.$end_time;

            $where_y['select_time'] = ['>=',strtotime($start_time)];
            $wheres_y['select_time'] = ['<=',strtotime($end_time)];
        }else{
            $time = '';
        }

        //诊疗总金额
        $diagnosis = Db::table('fa_diagnosis_order')
            ->where(['status'=>['in',[1,2]]])
            ->where($where)
            ->where($wheres)
            ->sum('reality_cost');

        //会员收费
        $vip = Db::table('fa_vip_cost')
            ->where($where)
            ->where($wheres)
            ->sum('money');

        //挂号费
        $registration = Db::table('fa_registration')
            ->where('status','已完成')
            ->where($where)
            ->where($wheres)
            ->sum('price');

        //周边产品收费
        $orders = Db::table('fa_orders')
            ->where('status',1)
            ->where($where)
            ->where($wheres)
            ->sum('money');

        //销售费用统计
        $order = Db::table('fa_diagnosis_order')
            ->where('entry',1)
            ->where($where_x)
            ->where($wheres_x)
            ->sum('totals');


        //诊疗成本统计
        $drug = Db::table('fa_drug')
            ->where($where)
            ->where($wheres)
            ->sum('total');

        //管理费用统计
        $administrative = Db::table('fa_administrative')
            ->where($where_y)
            ->where($wheres_y)
            ->sum('money');

        $diagnosis = number_format($diagnosis,2,'.','');
        $vip = number_format($vip,2,'.','');
        $registration = number_format($registration,2,'.','');
        $orders = number_format($orders,2,'.','');


        $order = number_format($order,2,'.','');
        $drug = number_format($drug,2,'.','');
        $administrative = number_format($administrative,2,'.','');

        //合计总收入
        $sum = $diagnosis+$vip+$registration+$orders;
        $sum = number_format($sum,2,'.','');

        //总支出统计
        $sums = $order+$drug+$administrative;
        $sums = number_format($sums,2,'.','');

        $this->assignconfig("diagnosis", $diagnosis);
        $this->assignconfig("vip", $vip);
        $this->assignconfig("registration", $registration);
        $this->assignconfig("orders", $orders);
        $this->assignconfig("sum", $sum);

        $this->assignconfig("order", $order);
        $this->assignconfig("drug", $drug);
        $this->assignconfig("administrative", $administrative);
        $this->assignconfig("sums", $sums);

        $this->view->assign("time", $time);

        return $this->view->fetch();
    }

    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign("row", $row->toArray());
        return $this->view->fetch();
    }
}
